<?php

namespace app\api\controller\service;

use app\common\controller\Api;
use app\common\model\Order as ModelOrder;
use app\common\model\Shop;
use app\common\model\order\Code;
use think\Hook;

/**
 * 服务点订单接口
 */
class Order extends Api
{

    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function list()
    {
        $get =  request()->get();
        extract($get);
        if (!isset($type) || empty($type)) {
            $this->error('参数错误');
        }
        $uid = $this->auth->id;
        $user = $this->auth->getUserinfo();
        $shop = Shop::get(['id' => $user['shop_id']]);
        if (!$shop) {
            $this->error('服务点不存在');
        }
        $order = new ModelOrder();
        $list = $order->with(['goods', 'address', 'shop',  'pickInfo', 'cleanInfo', 'retrieveInfo'])->where('order.status', $type)->where('order.shop_id', $shop['id'])->order('createtime desc')->paginate(10);
        $this->success('返回成功', $list);
    }
    /**
     * 详情
     *
     */
    public function info()
    {
        $get =  request()->get();
        extract($get);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $data = $order->get($id, ['goods', 'address', 'pickInfo', 'cleanInfo', 'retrieveInfo']);
        $this->success('返回成功', $data);
    }

    /**
     * 扫码分配
     *
     */
    public function distribution()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($sn) || empty($sn) || !isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $check = ModelOrder::get(['bar_code' => $sn]);
        if ($check) {
            $this->error('条码已分配');
        }
        if (isset($check['status']) && $check['status'] != 1) {
            $this->error('订单状态错误');
        }
        $res = $order->save(['bar_code' => $sn, 'status' => '2'], ['id' => $id]);
        if ($res) {
            $this->success('分配成功');
        }
        $this->error('分配失败');
    }
    /**
     * 扫码送回
     *
     */
    public function saoReturn()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id, ['shop','user']);
        if ($order['status'] != 6) {
            $this->error('订单状态错误');
        }
        $res = ModelOrder::where('id', $order['id'])->update(['status' => '7']);
        $code = $order->qh_code;
        $time = time();
        $ip = request()->ip();
        $sms = \app\common\model\Sms::create(['event' => 'notice', 'mobile' => $order->user->mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
        if ($res) {
            Hook::listen('sms_send', $sms, null, true);
            $this->success('送回成功');
        }
        $this->error('送回失败');
    }
    /**
     * 确认归还
     *
     */
    public function confirmReturn()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id);
        if ($order['status'] != 7) {
            $this->error('订单状态错误');
        }
        $res = $order->save(['status' => '8', 'taketime' => time()]);
        if ($res) {
            $this->success('归还成功');
        }
        $this->error('归还失败');
    }
}
